home *** CD-ROM | disk | FTP | other *** search
/ PC Open 102 / PC Open 102 CD 1.bin / CD1 / INTERNET / EMAIL / pop file / setup.exe / $_1_ / HTTP / Cookies.pm < prev    next >
Encoding:
Perl POD Document  |  2004-02-03  |  20.7 KB  |  773 lines

  1. package HTTP::Cookies;
  2.  
  3. use strict;
  4. use HTTP::Date qw(str2time time2str);
  5. use HTTP::Headers::Util qw(split_header_words join_header_words);
  6. use LWP::Debug ();
  7.  
  8. use vars qw($VERSION $EPOCH_OFFSET);
  9. $VERSION = sprintf("%d.%02d", q$Revision: 1.36 $ =~ /(\d+)\.(\d+)/);
  10.  
  11. # Legacy: because "use "HTTP::Cookies" used be the ONLY way
  12. #  to load the class HTTP::Cookies::Netscape.
  13. require HTTP::Cookies::Netscape;
  14.  
  15. $EPOCH_OFFSET = 0;  # difference from Unix epoch
  16. if ($^O eq "MacOS") {
  17.     require Time::Local;
  18.     $EPOCH_OFFSET = Time::Local::timelocal(0,0,0,1,0,70);
  19. }
  20.  
  21. # A HTTP::Cookies object is a hash.  The main attribute is the
  22. # COOKIES 3 level hash:  $self->{COOKIES}{$domain}{$path}{$key}.
  23.  
  24. sub new
  25. {
  26.     my $class = shift;
  27.     my $self = bless {
  28.     COOKIES => {},
  29.     }, $class;
  30.     my %cnf = @_;
  31.     for (keys %cnf) {
  32.     $self->{lc($_)} = $cnf{$_};
  33.     }
  34.     $self->load;
  35.     $self;
  36. }
  37.  
  38.  
  39. sub add_cookie_header
  40. {
  41.     my $self = shift;
  42.     my $request = shift || return;
  43.     my $url = $request->url;
  44.     my $scheme = $url->scheme;
  45.     unless ($scheme =~ /^https?\z/) {
  46.     LWP::Debug::debug("Will not add cookies to non-HTTP requests");
  47.     return;
  48.     }
  49.  
  50.     my $domain = _host($request, $url);
  51.     $domain = "$domain.local" unless $domain =~ /\./;
  52.     my $secure_request = ($scheme eq "https");
  53.     my $req_path = _url_path($url);
  54.     my $req_port = $url->port;
  55.     my $now = time();
  56.     _normalize_path($req_path) if $req_path =~ /%/;
  57.  
  58.     my @cval;    # cookie values for the "Cookie" header
  59.     my $set_ver;
  60.     my $netscape_only = 0; # An exact domain match applies to any cookie
  61.  
  62.     while ($domain =~ /\./) {
  63.  
  64.         LWP::Debug::debug("Checking $domain for cookies");
  65.     my $cookies = $self->{COOKIES}{$domain};
  66.     next unless $cookies;
  67.     if ($self->{delayload} && defined($cookies->{'//+delayload'})) {
  68.         my $cookie_data = $cookies->{'//+delayload'}{'cookie'};
  69.         delete $self->{COOKIES}{$domain};
  70.         $self->load_cookie($cookie_data->[1]);
  71.         $cookies = $self->{COOKIES}{$domain};
  72.         next unless $cookies;  # should not really happen
  73.     }
  74.  
  75.     # Want to add cookies corresponding to the most specific paths
  76.     # first (i.e. longest path first)
  77.     my $path;
  78.     for $path (sort {length($b) <=> length($a) } keys %$cookies) {
  79.             LWP::Debug::debug("- checking cookie path=$path");
  80.         if (index($req_path, $path) != 0) {
  81.             LWP::Debug::debug("  path $path:$req_path does not fit");
  82.         next;
  83.         }
  84.  
  85.         my($key,$array);
  86.         while (($key,$array) = each %{$cookies->{$path}}) {
  87.         my($version,$val,$port,$path_spec,$secure,$expires) = @$array;
  88.             LWP::Debug::debug(" - checking cookie $key=$val");
  89.         if ($secure && !$secure_request) {
  90.             LWP::Debug::debug("   not a secure requests");
  91.             next;
  92.         }
  93.         if ($expires && $expires < $now) {
  94.             LWP::Debug::debug("   expired");
  95.             next;
  96.         }
  97.         if ($port) {
  98.             my $found;
  99.             if ($port =~ s/^_//) {
  100.             # The correponding Set-Cookie attribute was empty
  101.             $found++ if $port eq $req_port;
  102.             $port = "";
  103.             }
  104.             else {
  105.             my $p;
  106.             for $p (split(/,/, $port)) {
  107.                 $found++, last if $p eq $req_port;
  108.             }
  109.             }
  110.             unless ($found) {
  111.                 LWP::Debug::debug("   port $port:$req_port does not fit");
  112.             next;
  113.             }
  114.         }
  115.         if ($version > 0 && $netscape_only) {
  116.             LWP::Debug::debug("   domain $domain applies to " .
  117.                       "Netscape-style cookies only");
  118.             next;
  119.         }
  120.  
  121.             LWP::Debug::debug("   it's a match");
  122.  
  123.         # set version number of cookie header.
  124.             # XXX: What should it be if multiple matching
  125.                 #      Set-Cookie headers have different versions themselves
  126.         if (!$set_ver++) {
  127.             if ($version >= 1) {
  128.             push(@cval, "\$Version=$version");
  129.             }
  130.             elsif (!$self->{hide_cookie2}) {
  131.             $request->header(Cookie2 => '$Version="1"');
  132.             }
  133.         }
  134.  
  135.         # do we need to quote the value
  136.         if ($val =~ /\W/ && $version) {
  137.             $val =~ s/([\\\"])/\\$1/g;
  138.             $val = qq("$val");
  139.         }
  140.  
  141.         # and finally remember this cookie
  142.         push(@cval, "$key=$val");
  143.         if ($version >= 1) {
  144.             push(@cval, qq(\$Path="$path"))     if $path_spec;
  145.             push(@cval, qq(\$Domain="$domain")) if $domain =~ /^\./;
  146.             if (defined $port) {
  147.             my $p = '$Port';
  148.             $p .= qq(="$port") if length $port;
  149.             push(@cval, $p);
  150.             }
  151.         }
  152.  
  153.         }
  154.         }
  155.  
  156.     } continue {
  157.     # Try with a more general domain, alternately stripping
  158.     # leading name components and leading dots.  When this
  159.     # results in a domain with no leading dot, it is for
  160.     # Netscape cookie compatibility only:
  161.     #
  162.     # a.b.c.net    Any cookie
  163.     # .b.c.net    Any cookie
  164.     # b.c.net    Netscape cookie only
  165.     # .c.net    Any cookie
  166.  
  167.     if ($domain =~ s/^\.+//) {
  168.         $netscape_only = 1;
  169.     }
  170.     else {
  171.         $domain =~ s/[^.]*//;
  172.         $netscape_only = 0;
  173.     }
  174.     }
  175.  
  176.     $request->header(Cookie => join("; ", @cval)) if @cval;
  177.  
  178.     $request;
  179. }
  180.  
  181.  
  182. sub extract_cookies
  183. {
  184.     my $self = shift;
  185.     my $response = shift || return;
  186.  
  187.     my @set = split_header_words($response->_header("Set-Cookie2"));
  188.     my @ns_set = $response->_header("Set-Cookie");
  189.  
  190.     return $response unless @set || @ns_set;  # quick exit
  191.  
  192.     my $request = $response->request;
  193.     my $url = $request->url;
  194.     my $req_host = _host($request, $url);
  195.     $req_host = "$req_host.local" unless $req_host =~ /\./;
  196.     my $req_port = $url->port;
  197.     my $req_path = _url_path($url);
  198.     _normalize_path($req_path) if $req_path =~ /%/;
  199.  
  200.     if (@ns_set) {
  201.     # The old Netscape cookie format for Set-Cookie
  202.         # http://www.netscape.com/newsref/std/cookie_spec.html
  203.     # can for instance contain an unquoted "," in the expires
  204.     # field, so we have to use this ad-hoc parser.
  205.     my $now = time();
  206.  
  207.     # Build a hash of cookies that was present in Set-Cookie2
  208.     # headers.  We need to skip them if we also find them in a
  209.     # Set-Cookie header.
  210.     my %in_set2;
  211.     for (@set) {
  212.         $in_set2{$_->[0]}++;
  213.     }
  214.  
  215.     my $set;
  216.     for $set (@ns_set) {
  217.         my @cur;
  218.         my $param;
  219.         my $expires;
  220.         for $param (split(/;\s*/, $set)) {
  221.         my($k,$v) = split(/\s*=\s*/, $param, 2);
  222.         if (defined $v) {
  223.             $v =~ s/\s+$//;
  224.             #print "$k => $v\n";
  225.         }
  226.         else {
  227.             $k =~ s/\s+$//;
  228.             #print "$k => undef";
  229.         }
  230.         my $lc = lc($k);
  231.         if ($lc eq "expires") {
  232.             my $etime = str2time($v);
  233.             if ($etime) {
  234.             push(@cur, "Max-Age" => str2time($v) - $now);
  235.             $expires++;
  236.             }
  237.         }
  238.         else {
  239.             push(@cur, $k => $v);
  240.         }
  241.         }
  242.         next if $in_set2{$cur[0]};
  243.  
  244. #        push(@cur, "Port" => $req_port);
  245.         push(@cur, "Discard" => undef) unless $expires;
  246.         push(@cur, "Version" => 0);
  247.         push(@cur, "ns-cookie" => 1);
  248.         push(@set, \@cur);
  249.     }
  250.     }
  251.  
  252.   SET_COOKIE:
  253.     for my $set (@set) {
  254.     next unless @$set >= 2;
  255.  
  256.     my $key = shift @$set;
  257.     my $val = shift @$set;
  258.  
  259.         LWP::Debug::debug("Set cookie $key => $val");
  260.  
  261.     my %hash;
  262.     while (@$set) {
  263.         my $k = shift @$set;
  264.         my $v = shift @$set;
  265.         my $lc = lc($k);
  266.         # don't loose case distinction for unknown fields
  267.         $k = $lc if $lc =~ /^(?:discard|domain|max-age|
  268.                                     path|port|secure|version)$/x;
  269.         if ($k eq "discard" || $k eq "secure") {
  270.         $v = 1 unless defined $v;
  271.         }
  272.         next if exists $hash{$k};  # only first value is signigicant
  273.         $hash{$k} = $v;
  274.     };
  275.  
  276.     my %orig_hash = %hash;
  277.     my $version   = delete $hash{version};
  278.     $version = 1 unless defined($version);
  279.     my $discard   = delete $hash{discard};
  280.     my $secure    = delete $hash{secure};
  281.     my $maxage    = delete $hash{'max-age'};
  282.     my $ns_cookie = delete $hash{'ns-cookie'};
  283.  
  284.     # Check domain
  285.     my $domain  = delete $hash{domain};
  286.     if (defined($domain)
  287.         && $domain ne $req_host && $domain ne ".$req_host") {
  288.         if ($domain !~ /\./ && $domain ne "local") {
  289.             LWP::Debug::debug("Domain $domain contains no dot");
  290.         next SET_COOKIE;
  291.         }
  292.         $domain = ".$domain" unless $domain =~ /^\./;
  293.         if ($domain =~ /\.\d+$/) {
  294.             LWP::Debug::debug("IP-address $domain illeagal as domain");
  295.         next SET_COOKIE;
  296.         }
  297.         my $len = length($domain);
  298.         unless (substr($req_host, -$len) eq $domain) {
  299.             LWP::Debug::debug("Domain $domain does not match host $req_host");
  300.         next SET_COOKIE;
  301.         }
  302.         my $hostpre = substr($req_host, 0, length($req_host) - $len);
  303.         if ($hostpre =~ /\./ && !$ns_cookie) {
  304.             LWP::Debug::debug("Host prefix contain a dot: $hostpre => $domain");
  305.         next SET_COOKIE;
  306.         }
  307.     }
  308.     else {
  309.         $domain = $req_host;
  310.     }
  311.  
  312.     my $path = delete $hash{path};
  313.     my $path_spec;
  314.     if (defined $path && $path ne '') {
  315.         $path_spec++;
  316.         _normalize_path($path) if $path =~ /%/;
  317.         if (!$ns_cookie &&
  318.                 substr($req_path, 0, length($path)) ne $path) {
  319.             LWP::Debug::debug("Path $path is not a prefix of $req_path");
  320.         next SET_COOKIE;
  321.         }
  322.     }
  323.     else {
  324.         $path = $req_path;
  325.         $path =~ s,/[^/]*$,,;
  326.         $path = "/" unless length($path);
  327.     }
  328.  
  329.     my $port;
  330.     if (exists $hash{port}) {
  331.         $port = delete $hash{port};
  332.         if (defined $port) {
  333.         $port =~ s/\s+//g;
  334.         my $found;
  335.         for my $p (split(/,/, $port)) {
  336.             unless ($p =~ /^\d+$/) {
  337.               LWP::Debug::debug("Bad port $port (not numeric)");
  338.             next SET_COOKIE;
  339.             }
  340.             $found++ if $p eq $req_port;
  341.         }
  342.         unless ($found) {
  343.             LWP::Debug::debug("Request port ($req_port) not found in $port");
  344.             next SET_COOKIE;
  345.         }
  346.         }
  347.         else {
  348.         $port = "_$req_port";
  349.         }
  350.     }
  351.     $self->set_cookie($version,$key,$val,$path,$domain,$port,$path_spec,$secure,$maxage,$discard, \%hash)
  352.         if $self->set_cookie_ok(\%orig_hash);
  353.     }
  354.  
  355.     $response;
  356. }
  357.  
  358. sub set_cookie_ok
  359. {
  360.     1;
  361. }
  362.  
  363.  
  364. sub set_cookie
  365. {
  366.     my $self = shift;
  367.     my($version,
  368.        $key, $val, $path, $domain, $port,
  369.        $path_spec, $secure, $maxage, $discard, $rest) = @_;
  370.  
  371.     # path and key can not be empty (key can't start with '$')
  372.     return $self if !defined($path) || $path !~ m,^/, ||
  373.                 !defined($key)  || $key  =~ m,^\$,;
  374.  
  375.     # ensure legal port
  376.     if (defined $port) {
  377.     return $self unless $port =~ /^_?\d+(?:,\d+)*$/;
  378.     }
  379.  
  380.     my $expires;
  381.     if (defined $maxage) {
  382.     if ($maxage <= 0) {
  383.         delete $self->{COOKIES}{$domain}{$path}{$key};
  384.         return $self;
  385.     }
  386.     $expires = time() + $maxage;
  387.     }
  388.     $version = 0 unless defined $version;
  389.  
  390.     my @array = ($version, $val,$port,
  391.          $path_spec,
  392.          $secure, $expires, $discard);
  393.     push(@array, {%$rest}) if defined($rest) && %$rest;
  394.     # trim off undefined values at end
  395.     pop(@array) while !defined $array[-1];
  396.  
  397.     $self->{COOKIES}{$domain}{$path}{$key} = \@array;
  398.     $self;
  399. }
  400.  
  401.  
  402. sub save
  403. {
  404.     my $self = shift;
  405.     my $file = shift || $self->{'file'} || return;
  406.     local(*FILE);
  407.     open(FILE, ">$file") or die "Can't open $file: $!";
  408.     print FILE "#LWP-Cookies-1.0\n";
  409.     print FILE $self->as_string(!$self->{ignore_discard});
  410.     close(FILE);
  411.     1;
  412. }
  413.  
  414.  
  415. sub load
  416. {
  417.     my $self = shift;
  418.     my $file = shift || $self->{'file'} || return;
  419.     local(*FILE, $_);
  420.     local $/ = "\n";  # make sure we got standard record separator
  421.     open(FILE, $file) or return;
  422.     my $magic = <FILE>;
  423.     unless ($magic =~ /^\#LWP-Cookies-(\d+\.\d+)/) {
  424.     warn "$file does not seem to contain cookies";
  425.     return;
  426.     }
  427.     while (<FILE>) {
  428.     next unless s/^Set-Cookie3:\s*//;
  429.     chomp;
  430.     my $cookie;
  431.     for $cookie (split_header_words($_)) {
  432.         my($key,$val) = splice(@$cookie, 0, 2);
  433.         my %hash;
  434.         while (@$cookie) {
  435.         my $k = shift @$cookie;
  436.         my $v = shift @$cookie;
  437.         $hash{$k} = $v;
  438.         }
  439.         my $version   = delete $hash{version};
  440.         my $path      = delete $hash{path};
  441.         my $domain    = delete $hash{domain};
  442.         my $port      = delete $hash{port};
  443.         my $expires   = str2time(delete $hash{expires});
  444.  
  445.         my $path_spec = exists $hash{path_spec}; delete $hash{path_spec};
  446.         my $secure    = exists $hash{secure};    delete $hash{secure};
  447.         my $discard   = exists $hash{discard};   delete $hash{discard};
  448.  
  449.         my @array =    ($version,$val,$port,
  450.              $path_spec,$secure,$expires,$discard);
  451.         push(@array, \%hash) if %hash;
  452.         $self->{COOKIES}{$domain}{$path}{$key} = \@array;
  453.     }
  454.     }
  455.     close(FILE);
  456.     1;
  457. }
  458.  
  459.  
  460. sub revert
  461. {
  462.     my $self = shift;
  463.     $self->clear->load;
  464.     $self;
  465. }
  466.  
  467.  
  468. sub clear
  469. {
  470.     my $self = shift;
  471.     if (@_ == 0) {
  472.     $self->{COOKIES} = {};
  473.     }
  474.     elsif (@_ == 1) {
  475.     delete $self->{COOKIES}{$_[0]};
  476.     }
  477.     elsif (@_ == 2) {
  478.     delete $self->{COOKIES}{$_[0]}{$_[1]};
  479.     }
  480.     elsif (@_ == 3) {
  481.     delete $self->{COOKIES}{$_[0]}{$_[1]}{$_[2]};
  482.     }
  483.     else {
  484.     require Carp;
  485.         Carp::carp('Usage: $c->clear([domain [,path [,key]]])');
  486.     }
  487.     $self;
  488. }
  489.  
  490.  
  491. sub clear_temporary_cookies
  492. {
  493.     my($self) = @_;
  494.  
  495.     $self->scan(sub {
  496.         if($_[9] or        # "Discard" flag set
  497.            not $_[8]) {    # No expire field?
  498.             $_[8] = -1;            # Set the expire/max_age field
  499.             $self->set_cookie(@_); # Clear the cookie
  500.         }
  501.       });
  502. }
  503.  
  504.  
  505. sub DESTROY
  506. {
  507.     my $self = shift;
  508.     $self->save if $self->{'autosave'};
  509. }
  510.  
  511.  
  512. sub scan
  513. {
  514.     my($self, $cb) = @_;
  515.     my($domain,$path,$key);
  516.     for $domain (sort keys %{$self->{COOKIES}}) {
  517.     for $path (sort keys %{$self->{COOKIES}{$domain}}) {
  518.         for $key (sort keys %{$self->{COOKIES}{$domain}{$path}}) {
  519.         my($version,$val,$port,$path_spec,
  520.            $secure,$expires,$discard,$rest) =
  521.                @{$self->{COOKIES}{$domain}{$path}{$key}};
  522.         $rest = {} unless defined($rest);
  523.         &$cb($version,$key,$val,$path,$domain,$port,
  524.              $path_spec,$secure,$expires,$discard,$rest);
  525.         }
  526.     }
  527.     }
  528. }
  529.  
  530.  
  531. sub as_string
  532. {
  533.     my($self, $skip_discard) = @_;
  534.     my @res;
  535.     $self->scan(sub {
  536.     my($version,$key,$val,$path,$domain,$port,
  537.        $path_spec,$secure,$expires,$discard,$rest) = @_;
  538.     return if $discard && $skip_discard;
  539.     my @h = ($key, $val);
  540.     push(@h, "path", $path);
  541.     push(@h, "domain" => $domain);
  542.     push(@h, "port" => $port) if defined $port;
  543.     push(@h, "path_spec" => undef) if $path_spec;
  544.     push(@h, "secure" => undef) if $secure;
  545.     push(@h, "expires" => HTTP::Date::time2isoz($expires)) if $expires;
  546.     push(@h, "discard" => undef) if $discard;
  547.     my $k;
  548.     for $k (sort keys %$rest) {
  549.         push(@h, $k, $rest->{$k});
  550.     }
  551.     push(@h, "version" => $version);
  552.     push(@res, "Set-Cookie3: " . join_header_words(\@h));
  553.     });
  554.     join("\n", @res, "");
  555. }
  556.  
  557. sub _host
  558. {
  559.     my($request, $url) = @_;
  560.     if (my $h = $request->header("Host")) {
  561.     $h =~ s/:\d+$//;  # might have a port as well
  562.     return $h;
  563.     }
  564.     return $url->host;
  565. }
  566.  
  567. sub _url_path
  568. {
  569.     my $url = shift;
  570.     my $path;
  571.     if($url->can('epath')) {
  572.        $path = $url->epath;    # URI::URL method
  573.     }
  574.     else {
  575.        $path = $url->path;           # URI::_generic method
  576.     }
  577.     $path = "/" unless length $path;
  578.     $path;
  579. }
  580.  
  581. sub _normalize_path  # so that plain string compare can be used
  582. {
  583.     my $x;
  584.     $_[0] =~ s/%([0-9a-fA-F][0-9a-fA-F])/
  585.              $x = uc($1);
  586.                  $x eq "2F" || $x eq "25" ? "%$x" :
  587.                                             pack("C", hex($x));
  588.               /eg;
  589.     $_[0] =~ s/([\0-\x20\x7f-\xff])/sprintf("%%%02X",ord($1))/eg;
  590. }
  591.  
  592. 1;
  593.  
  594. __END__
  595.  
  596. =head1 NAME
  597.  
  598. HTTP::Cookies - HTTP cookie jars
  599.  
  600. =head1 SYNOPSIS
  601.  
  602.   use HTTP::Cookies;
  603.   $cookie_jar = HTTP::Cookies->new(
  604.     file => "$ENV{'HOME'}/lwp_cookies.dat',
  605.     autosave => 1,
  606.   );
  607.  
  608.   use LWP;
  609.   my $browser = LWP::UserAgent->new;
  610.   $browser->cookie_jar($cookie_jar);
  611.  
  612. Or for an empty and temporary cookie jar:
  613.  
  614.   use LWP;
  615.   my $browser = LWP::UserAgent->new;
  616.   $browser->cookie_jar( {} );
  617.  
  618. =head1 DESCRIPTION
  619.  
  620. This class is for objects that represent a "cookie jar" -- that is, a
  621. database of all the HTTP cookies that a given LWP::UserAgent object
  622. knows about.
  623.  
  624. Cookies are a general mechanism which server side connections can use
  625. to both store and retrieve information on the client side of the
  626. connection.  For more information about cookies refer to
  627. <URL:http://www.netscape.com/newsref/std/cookie_spec.html> and
  628. <URL:http://www.cookiecentral.com/>.  This module also implements the
  629. new style cookies described in I<RFC 2965>.
  630. The two variants of cookies are supposed to be able to coexist happily.
  631.  
  632. Instances of the class I<HTTP::Cookies> are able to store a collection
  633. of Set-Cookie2: and Set-Cookie: headers and are able to use this
  634. information to initialize Cookie-headers in I<HTTP::Request> objects.
  635. The state of a I<HTTP::Cookies> object can be saved in and restored from
  636. files.
  637.  
  638. =head1 METHODS
  639.  
  640. The following methods are provided:
  641.  
  642. =over 4
  643.  
  644. =item $cookie_jar = HTTP::Cookies->new
  645.  
  646. The constructor takes hash style parameters.  The following
  647. parameters are recognized:
  648.  
  649.   file:            name of the file to restore cookies from and save cookies to
  650.   autosave:        save during destruction (bool)
  651.   ignore_discard:  save even cookies that are requested to be discarded (bool)
  652.   hide_cookie2:    do not add Cookie2 header to requests
  653.  
  654. Future parameters might include (not yet implemented):
  655.  
  656.   max_cookies               300
  657.   max_cookies_per_domain    20
  658.   max_cookie_size           4096
  659.  
  660.   no_cookies   list of domain names that we never return cookies to
  661.  
  662. =item $cookie_jar->add_cookie_header( $request )
  663.  
  664. The add_cookie_header() method will set the appropriate Cookie:-header
  665. for the I<HTTP::Request> object given as argument.  The $request must
  666. have a valid url attribute before this method is called.
  667.  
  668. =item $cookie_jar->extract_cookies( $response )
  669.  
  670. The extract_cookies() method will look for Set-Cookie: and
  671. Set-Cookie2: headers in the I<HTTP::Response> object passed as
  672. argument.  Any of these headers that are found are used to update
  673. the state of the $cookie_jar.
  674.  
  675. =item $cookie_jar->set_cookie( $version, $key, $val, $path, $domain, $port, $path_spec, $secure, $maxage, $discard, \%rest )
  676.  
  677. The set_cookie() method updates the state of the $cookie_jar.  The
  678. $key, $val, $domain, $port and $path arguments are strings.  The
  679. $path_spec, $secure, $discard arguments are boolean values. The $maxage
  680. value is a number indicating number of seconds that this cookie will
  681. live.  A value <= 0 will delete this cookie.  %rest defines
  682. various other attributes like "Comment" and "CommentURL".
  683.  
  684. =item $cookie_jar->save
  685.  
  686. =item $cookie_jar->save( $file )
  687.  
  688. This method file saves the state of the $cookie_jar to a file.
  689. The state can then be restored later using the load() method.  If a
  690. filename is not specified we will use the name specified during
  691. construction.  If the attribute I<ignore_discared> is set, then we
  692. will even save cookies that are marked to be discarded.
  693.  
  694. The default is to save a sequence of "Set-Cookie3" lines.
  695. "Set-Cookie3" is a proprietary LWP format, not known to be compatible
  696. with any browser.  The I<HTTP::Cookies::Netscape> sub-class can
  697. be used to save in a format compatible with Netscape.
  698.  
  699. =item $cookie_jar->load
  700.  
  701. =item $cookie_jar->load( $file )
  702.  
  703. This method reads the cookies from the file and adds them to the
  704. $cookie_jar.  The file must be in the format written by the save()
  705. method.
  706.  
  707. =item $cookie_jar->revert
  708.  
  709. This method empties the $cookie_jar and re-loads the $cookie_jar
  710. from the last save file.
  711.  
  712. =item $cookie_jar->clear
  713.  
  714. =item $cookie_jar->clear( $domain )
  715.  
  716. =item $cookie_jar->clear( $domain, $path )
  717.  
  718. =item $cookie_jar->clear( $domain, $path, $key )
  719.  
  720. Invoking this method without arguments will empty the whole
  721. $cookie_jar.  If given a single argument only cookies belonging to
  722. that domain will be removed.  If given two arguments, cookies
  723. belonging to the specified path within that domain are removed.  If
  724. given three arguments, then the cookie with the specified key, path
  725. and domain is removed.
  726.  
  727. =item $cookie_jar->clear_temporary_cookies
  728.  
  729. Discard all temporary cookies. Scans for all cookies in the jar
  730. with either no expire field or a true C<discard> flag. To be
  731. called when the user agent shuts down according to RFC 2965.
  732.  
  733. =item $cookie_jar->scan( \&callback )
  734.  
  735. The argument is a subroutine that will be invoked for each cookie
  736. stored in the $cookie_jar.  The subroutine will be invoked with
  737. the following arguments:
  738.  
  739.   0  version
  740.   1  key
  741.   2  val
  742.   3  path
  743.   4  domain
  744.   5  port
  745.   6  path_spec
  746.   7  secure
  747.   8  expires
  748.   9  discard
  749.  10  hash
  750.  
  751. =item $cookie_jar->as_string
  752.  
  753. =item $cookie_jar->as_string( $skip_discardables )
  754.  
  755. The as_string() method will return the state of the $cookie_jar
  756. represented as a sequence of "Set-Cookie3" header lines separated by
  757. "\n".  If $skip_discardables is TRUE, it will not return lines for
  758. cookies with the I<Discard> attribute.
  759.  
  760. =back
  761.  
  762. =head1 SEE ALSO
  763.  
  764. L<HTTP::Cookies::Netscape>, L<HTTP::Cookies::Microsoft>
  765.  
  766. =head1 COPYRIGHT
  767.  
  768. Copyright 1997-2002 Gisle Aas
  769.  
  770. This library is free software; you can redistribute it and/or
  771. modify it under the same terms as Perl itself.
  772.  
  773.